package se.webbzon.boltzmann.minigame;

import java.awt.Color;
import java.awt.Font;

import se.webbzon.boltzmann.minigame.sandbox.DefaultSandBoxItem;
import se.webbzon.boltzmann.minigame.sandbox.SandBox;
import se.webbzon.boltzmann.minigame.sandbox.SandBoxIcon;
import se.webbzon.boltzmann.minigame.sandbox.SandBoxObject;
import se.webbzon.boltzmann.resource.Resources;
import se.webbzon.oschi01.font.HorizontalAlignment;
import se.webbzon.oschi01.font.VerticalAlignment;
import se.webbzon.oschi01.worldarea.WorldArea;
import se.webbzon.oschi01.worldcamera.Rectangle;
import se.webbzon.oschi01.worldcamera.WorldCameraPane;
import se.webbzon.oschi01.worldobject.WorldObject;

public class InfoBar extends DefaultSandBoxItem {
	
	// The 2-dimensional depth of this info bar
	private static final int depth2d = 10;
	
	// The font used for this info bar
	private static final Font font = Resources.getFont("texgyrecursor_plain").deriveFont(14f);
	
	// The alignment of this info bar
	private HorizontalAlignment hAlign;
	private VerticalAlignment vAlign;
	
	// The sand box to which this info bar is attached
	private SandBox box;
	
	/*============================================================
	Constructors
	============================================================*/
	
	public InfoBar() {
		setInstance(new Instance());
		hAlign = HorizontalAlignment.RIGHT;
		vAlign = VerticalAlignment.BOTTOM;
	}
	
	/*============================================================
	Public Methods
	============================================================*/
	
	/** Returns the horizontal alignment of this info bar. **/
	public HorizontalAlignment getHorizontalAlignment() {
		return hAlign;
	}

	/** Sets the horizontal alignment of this info bar. **/
	public void setHorizontalAlignment(HorizontalAlignment hAlign) {
		this.hAlign = hAlign;
		SandBox box = this.box;
		if (box != null)
			updatePosition(box);
	}

	/** Returns the vertical alignment of this info bar. **/
	public VerticalAlignment getVerticalAlignment() {
		return vAlign;
	}

	/** Sets the vertical alignment of this score bar. **/
	public void setVerticalAlignment(VerticalAlignment vAlign) {
		this.vAlign = vAlign;
		SandBox box = this.box;
		if (box != null)
			updatePosition(box);
	}
	
	/*============================================================
	Private Methods
	============================================================*/
	
	/** Updates the position of the info bar so that it is positioned
	 * correctly inside the provided sand box. **/
	private void updatePosition(SandBox box) {
		java.awt.Rectangle r = new java.awt.Rectangle();
		box.getBoundary(r);
		final double x, y;
		switch (hAlign) {
		case LEFT: 		x = 8 + getWidth() / 2; break;
		case CENTER: 	x = r.getWidth(); break;
		default: 		x = r.getWidth() - getWidth() / 2 - 8; break;
		}
		switch (vAlign) {
		case TOP: 		y = 8 + getHeight() / 2; break;
		case MIDDLE: 	y = r.getHeight(); break;
		default: 		y = r.getHeight() - getHeight() / 2 - 8; break;
		}
		setPosition(x, y);
	}
	
	/*============================================================
	Private Classes
	============================================================*/
	
	private final class Instance extends SandBoxObject {

		public Instance() {
			super(WorldObject.onPostDraw, depth2d);
		}

		@Override public void onAttach(SandBox box) {
			InfoBar.this.box = box;
			updatePosition(box);
		}

		@Override public void onDetach(SandBox box) {
			InfoBar.this.box = null;
		}

		@Override public SandBoxIcon createIcon() {
			return null;
		}

		@Override public void onPostDraw(WorldArea world, WorldCameraPane hud) {
			SandBox box = InfoBar.this.box;
			if (box != null) {
				String text = box.getInfo().getText();
				if (text == null)
					text = box.getInfo().getDefaultText();
				if (text != null) {
					Rectangle r = new Rectangle();
					hud.setHAlignment(hAlign);
					hud.setVAlignment(vAlign);
					hud.setFont(font);
					int x = (int) getX();
					int y = (int) getY();
					hud.setTransparency(0.7f);
					hud.setColor(Color.white);
					hud.getBoundary(text, x, y, r);
					hud.fillRect(	(int) r.getX() - 2, (int) r.getY() - 2, 
							(int) r.getWidth() + 4, (int) r.getHeight() + 4);
					hud.setTransparency(1);
					hud.setColor(Color.black);
					hud.drawString(text, x, y);
				}
			}
		}
		
	}

}
